package com.xjrsoft.module.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageInput;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.UploadUtil;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.oss.factory.OssFactory;
import com.xjrsoft.module.system.dto.*;
import com.xjrsoft.module.system.entity.ExcelFile;
import com.xjrsoft.module.system.entity.File;
import com.xjrsoft.module.system.entity.FileManagement;
import com.xjrsoft.module.system.service.IFileManagementService;
import com.xjrsoft.module.system.vo.FileListVo;
import com.xjrsoft.module.system.vo.FileManagementPageVo;
import com.xjrsoft.module.system.vo.FileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文件管理表 前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2024-04-07
 */
@RestController
@RequestMapping(GlobalConstant.SYSTEM_MODULE_PREFIX +"/file-management")
@Api(value = GlobalConstant.SYSTEM_MODULE_PREFIX +"/file-management", tags = "文件管理")
@AllArgsConstructor
public class FileManagementController {

    private final IFileManagementService fileManagementService;

    @GetMapping("/page")
    @ApiOperation(value = "查询文件列表(分页)")
    public R page(@Valid PageInput dto) {
        PageOutput<FileManagementPageVo> pageList = fileManagementService.getPageList(dto);
        return R.ok(pageList);
    }

    @DeleteMapping
    @ApiOperation(value = "删除文件(可批量)")
    public R delete(@RequestBody List<Long> ids) {
        return R.ok(fileManagementService.removeBatchByIds(ids));
    }

    @PutMapping("update-file-name")
    @ApiOperation(value = "修改fileName")
    @Transactional(rollbackFor = Exception.class)
    public R updateFileName(@Valid @RequestBody UpdateExcelNameDto dto) {
        FileManagement fileManagement = BeanUtil.toBean(dto, FileManagement.class);
        return R.ok(fileManagementService.updateById(fileManagement));
    }

    @PostMapping
    @ApiOperation(value = "单文件上传")
    public R uploadFile(@RequestParam(value = "file", required = true) MultipartFile multipartFile) throws Exception {
        Long fileId = IdWorker.getId();
        String filename = multipartFile.getOriginalFilename();
        String suffix = StringUtils.substringAfterLast(filename, StringPool.DOT);
        //保存到云服务器
        String filePath = UploadUtil.uploadFileNew(multipartFile);

        FileManagement fileEntity = new FileManagement();
        fileEntity.setId(fileId);
        fileEntity.setFileName(filename);
        fileEntity.setFileUrl(filePath);
        fileEntity.setFileSize(multipartFile.getSize());
        fileEntity.setFileSuffiex(StringPool.DOT + suffix);
        fileEntity.setFileType(suffix);
        fileManagementService.save(fileEntity);
        return R.ok(filePath);
    }

    @PutMapping("update-file-auth")
    @ApiOperation(value = "修改file权限")
    @Transactional(rollbackFor = Exception.class)
    public R updateFileAuth(@Valid @RequestBody UpdateFileAuthDto dto) {
        return R.ok(fileManagementService.updateFileAuth(dto));
    }

    @GetMapping("/file-auth-info")
    @ApiOperation(value = "获取文件权限数据")
    public R fileAuthInfo(@RequestParam(required = true) Long id) {
        return R.ok(fileManagementService.fileAuthInfo(id));
    }

}
